// 引入SDK核心类，js文件根据自己业务，位置可自行放置
var QQMapWX = require('../../components/libs/qqmap-wx-jssdk');
var qqmapsdk;
// ... 其他代码保持不变 ...

Page({
  data: {
    title: '',
    address: [],
    markers: '',
    toiletList: [],// 初始化厕所列表
    longitude: '',
    latitude: '',
    userMarker: null // 用户当前位置标记
  },
  onLoad: function () {
    // 实例化API核心类
    qqmapsdk = new QQMapWX({
      key: 'RUOBZ-LLW6Q-5755D-4DD53-RCIS2-OSBSS'
    });
  },

  searchHotel: function () {
    // 调用接口
    qqmapsdk.search({
      keyword: '厕所',
      success: (res) => {
        console.log(res);
        const result = res.data.map(item => item.address)
        console.log(result)
        this.setData({
          address: result
        })
      },
      fail: function (res) {
        console.log(res);
      },
      complete: function (res) {
        console.log(res);
      }
    })
  },

  // 事件触发，调用接口
  nearby_search() {
    qqmapsdk.search({
      keyword: '厕所',  //搜索关键词
      location: {
        latitude: 21.271886,
        longitude: 110.35583
      },  //设置周边搜索中心点   {lat: 21.271886, lng: 110.35583}
      success: (res) => { //搜索成功后的回调
        let toiletList = []
        var mks = this.data.markers
        console.log("mks:", mks)
        for (var i = 0; i < res.data.length; i++) {
          mks.push({ // 获取返回结果，放到mks数组中
            title: res.data[i].title,
            id: res.data[i].id,
            latitude: res.data[i].location.lat,
            longitude: res.data[i].location.lng,
            width: 20,
            height: 20,
            callout: { //自定义标记点上方的气泡窗口 点击有效  
              content: res.data[i].title, //文本
              color: '#ffffff', //文字颜色
              fontSize: 15, //文本大小
              borderRadius: 15, //边框圆角
              padding: '10',
              bgColor: '#406390', //背景颜色
              display: 'BYCLICK', //常显
            }
          })
          toiletList.push({
            id: res.data[i].id,
            address: res.data[i].address,
            latitude: res.data[i].location.lat,
            longitude: res.data[i].location.lng
          })
        }
        console.log("mks:", mks)
        this.setData({
          markers: mks,
          toiletList: toiletList,
          latitude: toiletList[0].latitude,
          longitude: toiletList[0].longitude
        })
      },
      fail: function (res) {
        console.log(res);
      },
      complete: function (res) {
        console.log(res);
      }
    });
  },

  selectToilet(e) {
    const longitude = e.currentTarget.dataset.longitude
    const latitude = e.currentTarget.dataset.latitude
    this.setData({
      latitude: latitude,
      longitude: longitude
    })
  },

  location() {
    wx.getLocation({
      type: 'wgs84',
      success: res => {
        const latitude = res.latitude;
        const longitude = res.longitude;
        const mks = [];
        mks.push({
          title: '我的位置',
          id: 0,
          latitude: latitude,
          longitude: longitude,
          width: 20,
          height: 20,
          callout: {
            content: '我的位置',
            color: '#ffffff',
            fontSize: 15,
            borderRadius: 15,
            padding: '10',
            bgColor: '#406390',
            display: 'ALWAYS'
          }
        });
        this.setData({
          latitude: latitude,
          longitude: longitude,
          markers: mks
        });
      },
      fail: (err) => {
        console.error('获取位置失败:', err);
        if (err.errCode === 2) {
          wx.showModal({
            title: '提示',
            content: '请开启设备的定位服务以获取位置信息',
            success: (modalRes) => {
              if (modalRes.confirm) {
                wx.openSetting();
              }
            }
          });
        }
      }
    });
  },

  onShow() {
    const systemSetting = wx.getSystemSetting()
    if (!systemSetting.locationEnabled) {
      wx.showModal({
        title: '提示',
        content: '请开启 GPS 定位功能以获得更准确的位置信息',
        success: res => {
          if (res.confirm) {
            wx.openSetting({
              success: res => {
                console.log(res.authSetting)
                if (res.authSetting['scope.userLocation']) {
                  this.location()
                } else {
                  console.error('用户未开启定位权限');
                }
              }
            })
          }
        }
      })
    } else {
      this.location()
    }
  },

});